import os
import random
import time

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_data_deletion_cre_ascm_data_locator import AdminDataDeletionCreAscmDataLocator
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Test_data.menu_name_data import MenuName


class AdminDataDeletionCreAscmDataPage(Basepage):
    # CRE Data 页面URL
    cre_data_url = "https://testcreadmin.aligy.cn/console/singleCreData"
    # CRE&ASCM Data 页面URL
    cre_ascm_data_url = "https://testcreadmin.aligy.cn/console/creData"

    # 导入文件的地址
    import_success_file_path = "/home/seluser/testproject/Case/python_ui/Test_data/import_data_deletion/cre&ascmImportSuccess.xlsx"
    import_wrong_format_file_path = "/home/seluser/testproject/Case/python_ui/Test_data/import_data_deletion/cre&ascmImportWrongFormat.doc"
    local_import_success_file_path = "C:\code\hj\CloudTest-CRE_ui-20240327150242\Case\python_ui\Test_data\import_data_deletion\cre&ascmImportSuccess.xlsx"
    local_import_wrong_format_file_path = "C:\code\hj\CloudTest-CRE_ui-20240219175528\Case\python_ui\Test_data\import_data_deletion\cre&ascmImportWrongFormat.doc"

    def admin_login_by_unpermissioned_username(self, cmdopt, usr, pwd):
        doc = '使用没有权限的账号登录'
        self.driver.get(Commondata.url[cmdopt]['admin'])
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele, doc)
        time.sleep(2)
        self.wait_elevisble(Loginlocator.login_password_ele, doc)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, usr, doc)
        self.send_keys(Loginlocator.login_password_ele, pwd, doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code, Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele, doc)
        time.sleep(2)
        self.wait_elevisble(Loginlocator.email_verification_code, doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'], doc)
        time.sleep(2)
        self.click_element(Loginlocator.email_submit_ele, doc)
        self.wait_elevisble(Loginlocator.admin_home_page, doc)
        time.sleep(5)

    def enter_data_deletion_cre_ascm_data_page(self):
        time.sleep(2)
        doc = "进入CRE Data 菜单页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        # self.wait_elevisble(AdminDataDeletionCreDataLocator.data_deletion_menu, doc)
        # self.click_element(AdminDataDeletionCreDataLocator.data_deletion_menu, doc)
        time.sleep(2)
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.cre_ascm_data_menu, doc)
        self.click_element(AdminDataDeletionCreAscmDataLocator.cre_ascm_data_menu, doc)
        time.sleep(5)

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    def check_cre_permission(self):
        time.sleep(2)
        doc = "进入CRE Data 菜单页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        time.sleep(2)
        assert not self.element_is_exist(AdminDataDeletionCreAscmDataLocator.cre_data_menu), '能够找到元素'
        time.sleep(2)
        # 通过url访问
        self.driver.get(self.cre_data_url)
        time.sleep(5)
        assert self.element_is_exist(AdminDataDeletionCreAscmDataLocator.no_permission), '不能找到元素'

    def check_cre_ascm_permission(self):
        time.sleep(2)
        doc = "进入CRE&ASCM Data 菜单页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        time.sleep(2)
        assert not self.element_is_exist(AdminDataDeletionCreAscmDataLocator.cre_ascm_data_menu), '能够找到元素'
        time.sleep(2)
        # 通过url访问
        url = 'https://testcreadmin.aligy.cn/console/creData'
        self.driver.get(self.cre_ascm_data_url)
        time.sleep(5)
        assert self.element_is_exist(AdminDataDeletionCreAscmDataLocator.no_permission), '不能找到元素'

    def list_columns(self):
        doc = "CRE&ASCM Data 列表字段校验"

        list_columns = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_columns, doc)
        # 判断各字段名称
        assert list_columns[0].text == "Account Email"
        assert list_columns[1].find_element(By.XPATH, ".//span[@class='antd4-table-column-title']").text == "Status"
        assert list_columns[2].text.startswith("Creation Time")
        assert list_columns[3].find_element(By.XPATH,
                                            ".//span[@class='antd4-table-column-title']").text == "Payment Mode"
        assert list_columns[4].text == "Available Balance"
        assert list_columns[5].text == "Outstanding Amount"
        assert list_columns[6].text == "Settlement Currency"
        assert list_columns[7].find_element(By.XPATH,
                                            ".//span[@class='antd4-table-column-title']").text == "Account Type"
        assert list_columns[8].text == "User Name"
        assert list_columns[9].text == "Importer"
        assert list_columns[10].text.startswith("Import Time")
        assert list_columns[11].text.startswith("Actions")

        time.sleep(3)
        # 判断Status字段选项
        list_columns[1].find_element(By.XPATH, ".//span[contains(@class, 'antd4-dropdown-trigger')]").click()
        time.sleep(3)
        items = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_columns_items, doc)
        assert items[0].find_element(By.XPATH, ".//span[not(@class)]").text == "Undeleted"
        assert items[1].find_element(By.XPATH, ".//span[not(@class)]").text == "Deleting"
        assert items[2].find_element(By.XPATH, ".//span[not(@class)]").text == "Abnormal"
        assert items[3].find_element(By.XPATH, ".//span[not(@class)]").text == "Deleted"

        # 判断Payment Mode字段选项
        list_columns[3].find_element(By.XPATH, ".//span[contains(@class, 'antd4-dropdown-trigger')]").click()
        time.sleep(1)
        list_columns[3].find_element(By.XPATH, ".//span[contains(@class, 'antd4-dropdown-trigger')]").click()
        time.sleep(3)
        items = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_columns_items, doc)
        assert items[4].find_element(By.XPATH, ".//span[not(@class)]").text == "PrePaid"
        assert items[5].find_element(By.XPATH, ".//span[not(@class)]").text == "PostPaid"

        # 判断Account Type字段选项
        # list_columns[7].find_element(By.XPATH, ".//span[contains(@class, 'antd4-dropdown-trigger')]").click()
        self.click_by_js(AdminDataDeletionCreAscmDataLocator.list_filter3, doc)
        time.sleep(3)
        items = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_columns_items, doc)
        assert items[6].find_element(By.XPATH, ".//span[not(@class)]").text == "Enterprise"
        assert items[7].find_element(By.XPATH, ".//span[not(@class)]").text == "Personal"

        # 判断Actions字段的按钮
        action_items = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_actions_items, doc)
        assert action_items[0].find_element(By.XPATH, "./span").text == "Delete"
        assert action_items[1].find_element(By.XPATH, "./span").text == "Log"
        assert action_items[2].find_element(By.XPATH, "./span").text == "Remove"

    def check_prompt(self):
        doc = "CRE&ASCM Data 页面提示语校验"

        # 判断问号提示语
        self.move_mouse_to_element(AdminDataDeletionCreAscmDataLocator.prompt_icon_question_mark)
        time.sleep(2)
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.prompt_question_mark,
                                doc).text == "CRE&ASCM Data refers to the data of the main user dimension in CRE and its corresponding level-1 organization dimension in ASCM, which includes the order, bill, account, message notification, etc. of the main user of CRE and its corresponding ASCM level-1 organization and its cloud resources, resource sets, users, user groups, etc."
        # 判断叹号提示语
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.prompt_exclamation_mark,
                                doc).text == "Please download the template for data import first, and upload the Excel for import after filling in.Download Template"

    def download_import_template(self, cmdopt):
        doc = "下载导入模板"
        self.click_element(AdminDataDeletionCreAscmDataLocator.template_download, doc)

        time.sleep(2)
        file_name = "CRE_User_Import_Template"
        list_of_files = os.listdir(MenuName.download_path[cmdopt])
        latest_file = list_of_files[-1]
        assert latest_file.startswith(file_name) and latest_file.endswith(".xlsx")

    def import_success(self):
        doc = "导入数据功能校验"
        # 导入文件
        self.send_keys(AdminDataDeletionCreAscmDataLocator.import_file, self.import_success_file_path, doc)
        # 判断导入按钮不可点击
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.import_file_button_disabled, doc)
        # 判断导入过程的提示文字
        # 页面暂时缺失相关元素，待补充

        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.pup_success, doc)
        # 判断导入成功后的弹窗文字与提示文字
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.pup_success, doc).text == "Imported successfully."
        message_text = self.get_element(AdminDataDeletionCreAscmDataLocator.message_text, doc).text
        assert message_text.startswith("Recently imported") and message_text.endswith("data successfully.")

    def import_wrong_format(self):
        doc = "导入非\".xlsx, .xls, .csv\"文件类型"
        # 导入文件
        self.send_keys(AdminDataDeletionCreAscmDataLocator.import_file, self.import_wrong_format_file_path, doc)

        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.pup_notice, doc)
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.pup_notice,
                                doc).text == "Uploading this file format .doc is not supported, should be .xlsx, .xls, .csv"

    def remove_data(self, remove_list):

        doc = "移除 CRE&ASCM Data 列表数据"
        for data in remove_list:
            self.query_trigger(0, data)
            time.sleep(2)
            self.get_elements(AdminDataDeletionCreAscmDataLocator.list_actions_items, doc)[2].click()

            self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.pup_success, doc)
            assert self.get_element(AdminDataDeletionCreAscmDataLocator.pup_success,
                                    doc).text == "Successfully removed."
            self.click_element(AdminDataDeletionCreAscmDataLocator.query_reset_button, doc)

    def check_remove_state(self, status_options):
        doc = "核对列表数据 status 与 remove按钮状态对应情况"
        for status in status_options:
            self.filter_trigger(0, [status])
            rows = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)
            if status == "Undeleted":
                for data in rows:
                    assert not data.find_elements(By.XPATH, ".//button")[2].get_attribute('disabled')
            else:
                for data in rows:
                    assert data.find_elements(By.XPATH, ".//button")[2].get_attribute('disabled')

    def check_remove(self):
        doc = "remove功能校验"

        self.import_success()
        time.sleep(2)

        # 校对 status 和 remove 按钮的状态
        # self.check_remove_state(['Undeleted', 'Deleting', 'Abnormal', 'Deleted'])
        time.sleep(2)

        # 单个移除校对
        self.filter_trigger(0, ['Undeleted'])
        self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)[0].find_elements(By.XPATH,
                                                                                                    ".//button")[
            2].click()

        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.pup_success, doc)
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.pup_success,
                                doc).text == "Successfully removed."
        # 多选包含非undeleted状态的数据
        self.filter_trigger(0, ['Undeleted', 'Deleting'])
        rows = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)
        for data in rows:
            time.sleep(0.5)
            data.find_element(By.XPATH, './/label//input').click()
        time.sleep(1)
        assert self.get_elements(AdminDataDeletionCreAscmDataLocator.list_bitch_buttons, doc)[1].get_attribute(
            "disabled")

        # 批量移除
        self.filter_trigger(0, ['Undeleted'])
        rows = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)
        for data in rows:
            time.sleep(0.5)
            data.find_element(By.XPATH, './/label//input').click()
        time.sleep(1)
        self.get_elements(AdminDataDeletionCreAscmDataLocator.list_bitch_buttons, doc)[1].click()
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.pup_success, doc)
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.pup_success,
                                doc).text == "Successfully removed."

    def check_selector_options(self):
        doc = "查询条件选项校验"
        self.move_mouse_to_click_element(AdminDataDeletionCreAscmDataLocator.query_selector, doc)
        time.sleep(2)
        options = self.get_elements(AdminDataDeletionCreAscmDataLocator.query_options, doc)
        assert options[0].text == "Account Email"
        assert options[1].text == "User Name"
        assert options[2].text == "Importer"

    def query_trigger(self, type_index, keywords, if_reset=False):
        doc = "查询 CRE&ASCM Data 列表"
        time.sleep(3)
        self.move_mouse_to_click_element(AdminDataDeletionCreAscmDataLocator.query_selector, doc)
        time.sleep(2)
        self.get_elements(AdminDataDeletionCreAscmDataLocator.query_options, doc)[type_index].click()
        self.send_keys(AdminDataDeletionCreAscmDataLocator.query_input, keywords, doc)
        time.sleep(2)
        self.click_element(AdminDataDeletionCreAscmDataLocator.query_search_button, doc)
        time.sleep(3)
        self.check_query_results(type_index, keywords)
        if if_reset:
            self.click_element(AdminDataDeletionCreAscmDataLocator.query_reset_button, doc)

    def check_query_results(self, type_index, keyword):
        doc = "核对列表查询结果"
        rows = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)
        for data in rows:
            if type_index == 0:
                assert keyword in data.find_elements(By.XPATH, "./td[@class='antd4-table-cell']/div")[0].text
            elif type_index == 1:
                assert keyword in data.find_elements(By.XPATH, "./td[@class='antd4-table-cell']/div")[5].text
            elif type_index == 2:
                assert keyword in data.find_elements(By.XPATH, "./td[@class='antd4-table-cell']/div")[6].text

    def query_table(self, cmdopt):
        test_data = [
            Logindata.user_data[cmdopt]['user2']['username'],
            Logindata.user_data[cmdopt]['user3']['username'],
            Logindata.user_data[cmdopt]['user6']['username'],
        ]
        self.import_success()
        time.sleep(2)
        try:
            # account email 查询
            ae_fuzzy_keyword = "0908"
            ae_full_keyword = "testE1204@qq.com"
            self.query_trigger(0, ae_fuzzy_keyword, True)
            self.query_trigger(0, ae_full_keyword, True)

            # user name 查询
            un_fuzzy_keyword = "1183"
            un_full_keyword = "testE1204"
            self.query_trigger(1, un_fuzzy_keyword, True)
            self.query_trigger(1, un_full_keyword, True)

            # importer 查询
            im_fuzzy_keyword = "lwm"
            im_full_keyword = "lwm528675013@163.com"

            self.query_trigger(2, im_fuzzy_keyword, True)
            self.query_trigger(2, im_full_keyword, True)

            time.sleep(3)
        finally:
            self.remove_data(test_data)

    def filter_trigger(self, filter_type: int, options, if_reset=False):
        self.refresh_page()
        time.sleep(3)
        doc = "字段筛选 CRE&ASCM Data 列表数据"
        if filter_type in [0, 1]:
            self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filters, doc)[filter_type].click()
        else:
            self.click_by_js(AdminDataDeletionCreAscmDataLocator.list_filter3, doc)

        if filter_type == 0:
            time.sleep(2)
            for option in options:
                if option == "Undeleted":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[0].click()
                    time.sleep(0.5)
                elif option == "Deleting":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[1].click()
                    time.sleep(0.5)
                elif option == "Abnormal":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[2].click()
                    time.sleep(0.5)
                elif option == "Deleted":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[3].click()
                    time.sleep(0.5)
                else:
                    continue
        elif filter_type == 1:
            for option in options:
                if option == "PrePaid":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[0].click()
                    time.sleep(0.5)
                elif option == "PostPaid":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[1].click()
                    time.sleep(0.5)
                else:
                    continue
        elif filter_type == 2:
            for option in options:
                if option == "Enterprise":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[0].click()
                    time.sleep(0.5)
                elif option == "Personal":
                    self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filter_options, doc)[1].click()
                    time.sleep(0.5)
                else:
                    continue
        time.sleep(1)
        self.click_element(AdminDataDeletionCreAscmDataLocator.list_filter_confirm, doc)
        time.sleep(2)
        self.check_filter_results(filter_type, options)
        if if_reset:
            print(filter_type)
            if filter_type == 2:
                js = "document.getElementsByClassName('antd4-table-content')[0].scrollLeft=1000"
                self.excute_js(js)
            self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filters, doc)[filter_type].click()
            time.sleep(1)
            self.click_element(AdminDataDeletionCreAscmDataLocator.list_filter_reset, doc)
            time.sleep(1)
            self.click_element(AdminDataDeletionCreAscmDataLocator.list_filter_confirm, doc)
            time.sleep(2)
            self.get_elements(AdminDataDeletionCreAscmDataLocator.list_filters, doc)[filter_type].click()
            time.sleep(1)
            try:
                self.get_element(AdminDataDeletionCreAscmDataLocator.list_filter_options_selected, doc)
                assert False
            except NoSuchElementException:
                assert True

    def check_filter_results(self, filter_type: int, options):
        doc = "核对列表筛选结果"
        rows = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)
        if len(options) != 0:
            for data in rows:
                if filter_type == 0:
                    assert data.find_element(By.XPATH, ".//span[@class='antd4-badge-status-text']").text in options
                elif filter_type == 1:
                    assert data.find_elements(By.XPATH, ".//div[@class='cre-table-nowrap']")[1].text in options
                elif filter_type == 2:
                    assert data.find_elements(By.XPATH, ".//div[@class='cre-table-nowrap']")[3].text in ['P', 'C']

    def filter_table(self, cmdopt):

        test_data = [
            Logindata.user_data[cmdopt]['user2']['username'],
            Logindata.user_data[cmdopt]['user3']['username'],
            Logindata.user_data[cmdopt]['user6']['username'],
        ]
        self.import_success()
        time.sleep(2)

        js = "document.getElementsByClassName('antd4-table-content')[0].scrollLeft=1000"
        self.excute_js(js)

        try:
            # status 字段筛选
            status_options = ['Undeleted', 'Deleting', 'Abnormal', 'Deleted']
            self.filter_trigger(0, ['Undeleted'])
            self.filter_trigger(0, ['Deleting'])
            self.filter_trigger(0, ['Abnormal'])
            self.filter_trigger(0, ['Deleted'])
            self.filter_trigger(0, ['Undeleted', 'Deleting'])
            self.filter_trigger(0, ['Undeleted', 'Deleting', 'Abnormal'])
            self.filter_trigger(0, status_options, True)

            # payment mode 字段筛选
            payment_mode_options = ['PrePaid', 'PostPaid']
            self.filter_trigger(1, ['PrePaid'])
            self.filter_trigger(1, ['PostPaid'])
            self.filter_trigger(1, payment_mode_options, True)

            # account type 字段筛选
            account_type_options = ['Enterprise', 'Personal']
            self.filter_trigger(2, ['Enterprise'])
            self.filter_trigger(2, ['Personal'])
            self.filter_trigger(2, account_type_options, True)
            time.sleep(3)
        finally:
            self.remove_data(test_data)

    def check_log_cre_bi(self):
        doc = "查看log-cre的basic information"
        status_options = ['Deleting', 'Abnormal', 'Deleted']
        self.filter_trigger(0, random.sample(status_options, 1))
        time.sleep(2)
        first_row_content = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)[0].find_elements(
            By.XPATH, "./td[@class='antd4-table-cell']")

        self.click_element(AdminDataDeletionCreAscmDataLocator.list_active_log, doc)

        time.sleep(2)
        # 判断 log bi 的内容和列表数据是否相等
        log_bi_items = self.get_elements(AdminDataDeletionCreAscmDataLocator.log_bi_items, doc)
        # account_email
        assert log_bi_items[0].text == first_row_content[0].text
        # status
        assert log_bi_items[1].text == first_row_content[1].text
        # creation_time
        assert log_bi_items[2].text == first_row_content[2].text
        # payment_mode
        assert log_bi_items[3].text == first_row_content[3].text
        # available_balance
        available_balance = first_row_content[4].text if first_row_content[4].text != "-" else "0.00"
        assert log_bi_items[4].text == available_balance
        # outstanding_amount
        outstanding_amount = first_row_content[5].text if first_row_content[5].text != "-" else "0.00"
        assert log_bi_items[5].text == outstanding_amount
        # account_type
        assert log_bi_items[6].text == first_row_content[7].text
        # user_name
        assert log_bi_items[7].text == first_row_content[8].text
        # importer
        assert log_bi_items[8].text == first_row_content[9].text
        # import_time
        assert log_bi_items[9].text == first_row_content[10].text

    def check_Log_cre_oi(self):
        doc = "查看log-cre的operational information"

        email = "postpaid0914@qq.com"
        self.query_trigger(0, email)
        time.sleep(2)

        self.click_element(AdminDataDeletionCreAscmDataLocator.list_active_log, doc)
        time.sleep(2)
        # 判断列表字段
        oi_table_columns = self.get_elements(AdminDataDeletionCreAscmDataLocator.drawer_oi_table, doc)
        assert oi_table_columns[0].text == "Operation ID"
        assert oi_table_columns[1].text == "Operator"
        assert oi_table_columns[2].text == "Operator IP"
        assert oi_table_columns[3].text.startswith("Operation Time")
        assert oi_table_columns[4].text == "Operation Result"
        # 判断排序
        doc = '判断列表是否是按operation id倒序'
        search_num = int(
            self.get_element(AdminDataDeletionCreAscmDataLocator.log_detail_oi_operation_num, doc).text.split(' ', 2)[
                1])
        operation_id = []
        if search_num <= 10:
            for j in range(1, search_num + 1):
                locate = "//div[contains(@class,'antd4-table antd4-table-scroll-horizontal')]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
            # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"
        if search_num > 10:
            for j in range(1, 11):
                locate = "//div[contains(@class,'antd4-table antd4-table-scroll-horizontal')]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
                # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"

    def check_log_cre_ddd(self):
        doc = "查看log-cre的data deletion detail"

        email = "postpaid0914@qq.com"
        self.query_trigger(0, email)
        time.sleep(2)

        self.click_element(AdminDataDeletionCreAscmDataLocator.list_active_log, doc)
        time.sleep(2)

        self.scroll_bar_operation(AdminDataDeletionCreAscmDataLocator.list_scroll)

        js = "document.getElementsByClassName('antd4-table-content')[2].scrollLeft=1000"
        self.excute_js(js)

        self.scroll_bar_operation(AdminDataDeletionCreAscmDataLocator.drawer_ddd_table)
        ddd_table_columns = self.get_elements(AdminDataDeletionCreAscmDataLocator.drawer_ddd_table, doc)
        time.sleep(2)
        assert ddd_table_columns[0].text == "Operation ID"
        assert ddd_table_columns[1].text == "Data Type"
        assert ddd_table_columns[2].text == "Status"
        assert ddd_table_columns[3].text.startswith("Start Time")
        assert ddd_table_columns[4].text.startswith("End Time")
        doc = '判断列表是否是按operation id倒序'
        search_num = int(
            self.get_element(AdminDataDeletionCreAscmDataLocator.log_detail_ddd_operation_num, doc).text.split(' ', 2)[
                1])
        operation_id = []
        if search_num <= 10:
            for j in range(1, search_num + 1):
                locate = "//div[contains(@class,'antd4-table antd4-table-ping-right antd4-table-scroll-horizontal')]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
            # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"
        if search_num > 10:
            for j in range(1, 11):
                locate = "//div[contains(@class,'antd4-table antd4-table-ping-right antd4-table-scroll-horizontal')]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
                # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"

    def check_log_ascm_bi(self):
        doc = "查看log-ascm的basic information"

        email = "postpaid0914@qq.com"
        self.query_trigger(0, email)
        time.sleep(2)

        first_row_content = self.get_elements(AdminDataDeletionCreAscmDataLocator.list_data_rows, doc)[0].find_elements(
            By.XPATH, "./td[@class='antd4-table-cell']")

        self.click_element(AdminDataDeletionCreAscmDataLocator.list_active_log, doc)
        time.sleep(2)
        self.get_elements(AdminDataDeletionCreAscmDataLocator.drawer_tabs, doc)[1].click()

        # 判断 log bi 的内容和列表数据是否相等
        log_bi_items = self.get_elements(AdminDataDeletionCreAscmDataLocator.log_bi_items, doc)

        # level-1 organization name
        assert log_bi_items[10].text == first_row_content[8].text
        # status
        assert log_bi_items[11].text == first_row_content[1].text
        # creation_time
        assert log_bi_items[12].text == first_row_content[2].text
        # importer
        assert log_bi_items[13].text == first_row_content[9].text
        # import_time
        assert log_bi_items[14].text == first_row_content[10].text

    def check_log_ascm_oi(self):
        doc = "查看log-ascm的operational information"

        email = "postpaid0914@qq.com"
        self.query_trigger(0, email)
        time.sleep(2)

        self.click_element(AdminDataDeletionCreAscmDataLocator.list_active_log, doc)
        time.sleep(2)
        self.get_elements(AdminDataDeletionCreAscmDataLocator.drawer_tabs, doc)[1].click()
        time.sleep(2)
        # 判断列表字段
        oi_table_columns = self.get_elements(AdminDataDeletionCreAscmDataLocator.drawer_oi_table, doc)
        assert oi_table_columns[5].text == "Operation ID"
        assert oi_table_columns[6].text == "Operator"
        assert oi_table_columns[7].text == "Operator IP"
        assert oi_table_columns[8].text.startswith("Operation Time")
        assert oi_table_columns[9].text == "Operation Result"
        # 判断排序
        doc = '判断列表是否是按operation id倒序'
        for i in self.get_elements(AdminDataDeletionCreAscmDataLocator.log_detail_oi_operation_num, doc):
            if i.text != '':
                search_num = int(i.text.split(' ', 2)[1])
                break
            else:
                continue

        operation_id = []
        if search_num <= 10:
            for j in range(1, search_num + 1):
                locate = "(//div[contains(@class,'antd4-table antd4-table-scroll-horizontal')])[2]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
            # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"
        if search_num > 10:
            for j in range(1, 11):
                locate = "(//div[contains(@class,'antd4-table antd4-table-scroll-horizontal')])[2]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
                # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"

    def check_log_ascm_ddd(self):
        doc = "查看log-ascm的data deletion detail"

        email = "postpaid0914@qq.com"
        self.query_trigger(0, email)
        time.sleep(2)

        self.click_element(AdminDataDeletionCreAscmDataLocator.list_active_log, doc)
        time.sleep(2)

        self.get_elements(AdminDataDeletionCreAscmDataLocator.drawer_tabs, doc)[1].click()
        time.sleep(2)

        self.scroll_bar_operation(AdminDataDeletionCreAscmDataLocator.list_scroll)

        js = "document.getElementsByClassName('antd4-table-content')[4].scrollLeft=1000"
        self.excute_js(js)

        self.scroll_bar_operation(AdminDataDeletionCreAscmDataLocator.drawer_ddd_table)
        ddd_table_columns = self.get_elements(AdminDataDeletionCreAscmDataLocator.drawer_ddd_table, doc)
        time.sleep(2)
        assert ddd_table_columns[7].text == "Operation ID"
        assert ddd_table_columns[8].text == "Data Type"
        assert ddd_table_columns[9].text == "Value"
        assert ddd_table_columns[10].text == "Status"
        assert ddd_table_columns[11].text.startswith("Start Time")
        assert ddd_table_columns[12].text.startswith("End Time")
        # doc = '判断列表是否是按operation id倒序'

        for i in self.get_elements(AdminDataDeletionCreAscmDataLocator.log_detail_ascm_ddd_pagin, doc):
            if i.text != '':
                search_num = int(i.text.split(' ', 2)[1])
                break
            else:
                continue
        operation_id = []
        if search_num <= 10:
            for j in range(1, search_num + 1):
                locate = "(//div[contains(@class,'antd4-table') and  contains(@class, ' antd4-table-scroll-horizonta')])[5]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
            # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"
        if search_num > 10:
            for j in range(1, 11):
                locate = "(//div[contains(@class,'antd4-table') and  contains(@class, ' antd4-table-scroll-horizonta')])[5]//tr[$_1]/td[1]/div".replace(
                    '$_1', str(j + 1))
                log_detail_oi_operation_id = (By.XPATH, locate)
                id = self.get_element(log_detail_oi_operation_id, doc).text
                operation_id.append(id)
                # 先将list正序，再倒叙，然后判断原始的list和倒叙的是否一致
            assert operation_id == list(reversed(sorted(operation_id))), "operation id 不是倒序"

    # =============================================== ASCM Data Page =============================================

    def enter_ascm_data(self):
        doc = "进入ASCM Data页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        time.sleep(2)
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.ascm_data_tab, doc)
        self.click_element(AdminDataDeletionCreAscmDataLocator.ascm_data_tab, doc)
        time.sleep(5)

    def ascm_download_import_template(self, cmdopt):
        doc = "下载ASCM导入模板"
        self.enter_ascm_data()
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.template_download, doc)
        self.click_by_js(AdminDataDeletionCreAscmDataLocator.template_download, doc)
        time.sleep(5)

        file_name = "ASCM_Organization_Import_Template"
        list_of_files = os.listdir(MenuName.download_path[cmdopt])
        latest_file = list_of_files[-1]
        assert latest_file.startswith(file_name) and latest_file.endswith(".xlsx")

    def ascm_check_import_format(self):
        doc = "ASCM导入非\".xlsx, .xls, .csv\"文件类型"
        self.enter_ascm_data()
        self.send_keys(AdminDataDeletionCreAscmDataLocator.import_file, self.import_wrong_format_file_path, doc)

        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.pup_notice, doc)
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.pup_notice,
                                doc).text == "Uploading this file format .doc is not supported, should be .xlsx, .xls, .csv"

    # =============================================== CRE Data Page =============================================

    def enter_cre_data(self):
        doc = "进入CRE Data页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        time.sleep(2)
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.cre_data_menu, doc)
        self.click_element(AdminDataDeletionCreAscmDataLocator.cre_data_menu, doc)
        time.sleep(5)

    def cre_download_import_template(self, cmdopt):
        doc = "下载CRE导入模板"
        self.enter_cre_data()
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.template_download, doc)
        self.click_by_js(AdminDataDeletionCreAscmDataLocator.template_download, doc)
        time.sleep(5)

        file_name = "CRE_User_Import_Template"
        list_of_files = os.listdir(MenuName.download_path[cmdopt])
        latest_file = list_of_files[-1]
        assert latest_file.startswith(file_name) and latest_file.endswith(".xlsx")

    def cre_check_import_format(self):
        doc = "CRE导入非\".xlsx, .xls, .csv\"文件类型"
        self.enter_ascm_data()
        self.send_keys(AdminDataDeletionCreAscmDataLocator.import_file, self.import_wrong_format_file_path, doc)

        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.pup_notice, doc)
        assert self.get_element(AdminDataDeletionCreAscmDataLocator.pup_notice,
                                doc).text == "Uploading this file format .doc is not supported, should be .xlsx, .xls, .csv"

    # =============================================== Document Center Page =============================================

    def enter_document_center(self):
        doc = "进入Document Center页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        time.sleep(2)
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.document_tab, doc)
        self.click_element(AdminDataDeletionCreAscmDataLocator.document_tab, doc)
        time.sleep(2)
        self.click_element(AdminDataDeletionCreAscmDataLocator.document_center_tab, doc)
        time.sleep(5)

    def document_center_query_document(self):
        doc = "Document Center查询文件"
        input_keyword = "api"
        self.enter_document_center()
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.document_center_loading_anime, doc)
        time.sleep(2)

        self.driver.switch_to.frame(self.get_element(AdminDataDeletionCreAscmDataLocator.document_center_frame, doc))
        # time.sleep(15)
        self.wait_elevisble(AdminDataDeletionCreAscmDataLocator.document_center_query_input, doc)
        self.send_keys(AdminDataDeletionCreAscmDataLocator.document_center_query_input, input_keyword, doc)

        time.sleep(2)
        associate_items = self.get_elements(AdminDataDeletionCreAscmDataLocator.document_center_query_associate_item, doc)
        assert len(associate_items) >= 1






